/*
 *   Copyright 2008 Francois Fernandes
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.opendoclibrary.server.internal;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import com.googlecode.opendoclibrary.library.spi.ILibrary;
import com.googlecode.opendoclibrary.server.ILibraryServer;
import com.googlecode.opendoclibrary.server.internal.core.LibraryServer;

public class LibraryInstanceTracker implements ServiceTrackerCustomizer
{
    private Logger log = Logger.getLogger( LibraryInstanceTracker.class );

    private final BundleContext bundleContext;

    /**
     * @param context
     * @param server
     */
    public LibraryInstanceTracker( BundleContext context )
    {
        this.bundleContext = context;
    }

    @Override
    public Object addingService( ServiceReference reference )
    {
        Object name = reference.getProperty( ILibrary.NAME );
        if ( name == null || !( name instanceof String ) || isEmpty( (String) name ) )
        {
            log.error( "found library reference without 'library.name' set. Ignoring" );
            return null;
        }
        ILibrary library = (ILibrary) bundleContext.getService( reference );
        if ( library == null )
        {
            log.error( "recieved library registration but library service retrieval failed." );
            return null;
        }
        ILibraryServer server = new LibraryServer( library );
        Properties props = new Properties();
        props.put( ILibrary.NAME, name );
        return bundleContext.registerService( ILibraryServer.class.getName(), server, props );
    }

    /**
     * @param tmp
     * @return
     */
    private boolean isEmpty( String s )
    {
        return s == null || s.trim().length() == 0;
    }

    @Override
    public void modifiedService( ServiceReference reference, Object service )
    {
        // nothing to do so far
    }

    @Override
    public void removedService( ServiceReference reference, Object service )
    {
        if ( service != null && service instanceof ServiceRegistration )
        {
            ServiceRegistration reg = (ServiceRegistration) service;
            reg.unregister();
        }
    }

}
